<template>
  <div style="padding: 10px">
<!--    功能区域-->
    <div style="margin: 10px 0">
      <el-button type="primary" @click="add">新增</el-button>
    </div>
<!--    搜索区域-->
    <div style="margin: 10px 0">
      <el-input v-model="search" style="width: 240px" placeholder="请输入关键字" clearable />
      <el-button type="primary" style="margin-left: 5px" @click="load()" >查询</el-button>
    </div>
    <el-table :data="tableData" border stripe style="width: 100%">
      <el-table-column prop="id" label="用户编号" sortable=""/>
      <el-table-column prop="username" label="用户名" />
      <el-table-column prop="sex" label="性别" />
      <el-table-column prop="age" label="年龄"/>
      <el-table-column prop="location" label="地址"/>
      <el-table-column fixed="right" label="操作" width="160">
        <template #default>
          <el-button size="mini" @click="handleEdit(scope.row)">
            编辑
          </el-button>
          <el-popconfirm title="确认删除吗?">
            <template #reference>
              <el-button  size="mini" type="danger" @click="handleDelete">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div style="padding: 10px 0">
      <el-pagination
          v-model:current-page="pageNum"
          :page-sizes="[5,10,20]"
          :size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
      />
      <el-dialog v-model="dialogVisible" title="新增用户" width="500">
        <el-form :model="form" label-width="120px">
          <el-form-item label="用户名">
            <el-input v-model="form.username" style="width: 80%"/>
          </el-form-item>
          <el-form-item label="年龄">
            <el-input  v-model="form.age" style="width: 80%"/>
          </el-form-item>
          <el-form-item label="性别">
            <el-radio-group v-model="form.sex" class="ml-4">
              <el-radio value="男">男</el-radio>
              <el-radio value="女">女</el-radio>
              <el-radio value="未知">未知</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="地址">
            <el-input type="textarea" v-model="form.location" style="width: 80%"/>
          </el-form-item>
        </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button @click="dialogVisible = false">取消</el-button>
            <el-button type="primary" @click="save">确定</el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
// @ is an alias to /src


import request from "@/utils/request";

export default {
  name: 'HomeView',
  components: {

  },
  data(){
    return{
      search:'',
      form:{},
      dialogVisible:false,
      pageNum:1,
      pageSize:20,
      total: 0,
      tableData: [

      ]
    }
  },
  created() {
    this.load()
  },
  methods:{
    handleEdit(row){
      this.form = JSON.parse(JSON.stringify(row))
      this.dialogVisible = true
    },
    handleDelete(){

    },
    handleSizeChange(){

    },
    handleCurrentChange(){

    },
    add(){
      this.dialogVisible=true
      this.form={}
    },
    save(){
      request.post("/user", this.form).then(res=>{
        this.dialogVisible = false

      })
    },
    //前端get方法用param传参
    load(){
      request.get("/user",{
        params: {
          pageSize: this.pageSize,
          pageNum: this.pageNum,
          search: this.search
        }
      }).then(res => {
        console.log(res)
        this.tableData  = res.data.records
      })
    }
  }
}
</script>
